﻿//------------------------------------------------------------------------------
// System Name:    Customer Service Platform
// Component:      Customer Search Page
// Created By:     Tingrong Shi (trshi@wicresoft.com)
// Created Date:   2011-2-14
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASBCC.Business;
using ASBCC.Business.CIS;
using ASBCC.Database.EntityFramework;

namespace BEGDC.CC.Web.CIS2.Overseas.Search
{
    public partial class CustomerSearch : System.Web.UI.Page
    {
        private static string Search = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            this.edsCustomer.Where = Search;
            if (!this.IsPostBack)
                Bind();
        }


        private void Bind()
        {
            //get roles
            var roles = (this.User as CSPrincipal).Role2s;
            string allrole = "";
            if (roles != null && roles.Count() != 0)
            {
                foreach (var role in roles)
                {
                    allrole += role;
                    allrole += ",";
                }
                allrole = allrole.Substring(0, allrole.Length - 2);
            }
            // selectlevel
            selectlevel.DataSource = new[] {
                new { Value = 0, Text = "--请选择--", },
                new { Value = 1, Text = "Level1", },
                new { Value = 2, Text = "Level2", },
                new { Value = 3, Text = "Level3", },
                new { Value = 4, Text = "Level4", },
            };
            selectlevel.DataValueField = "Value";
            selectlevel.DataTextField = "Text";
            selectlevel.DataBind();

            //selectCountry
            selectCountry.DataSource = SearchHelper.GetCountryByRoleIDs(null,allrole);
            selectCountry.DataValueField = "CountryID";
            selectCountry.DataTextField = "Name";
            selectCountry.DataBind();
            selectCountry.Items.Insert(0, new ListItem("--请选择--", "0"));

            //selectDefination
            selectDefination.DataSource = SearchHelper.GetDefinitions();
            selectDefination.DataValueField = "DefID";
            selectDefination.DataTextField = "Name";
            selectDefination.DataBind();
            selectDefination.Items.Insert(0, new ListItem("--请选择--", "0"));


            //selectDegree
            selectDegree.DataSource = new[] {
                new { Value = 0, Text = "--请选择--", },
                new { Value = 1, Text = "大专以下", },
                new { Value = 2, Text = "大专", },
                new { Value = 3, Text = "本科", },
                new { Value = 4, Text = "硕士", },
                new { Value = 5, Text = "博士", },
                new { Value = 6, Text = "MBA", },
                new { Value = 7, Text = "EMBA", },
                new { Value = 8, Text = "DBA", },
            };
            selectDegree.DataValueField = "Value";
            selectDegree.DataTextField = "Text";
            selectDegree.DataBind();

            //PreferenceList
            PreferenceList.DataSource = SearchHelper.GetPreferenceLists();
            PreferenceList.DataValueField = "ListID";
            PreferenceList.DataTextField = "Name";
            PreferenceList.DataBind();
            PreferenceList.Items.Insert(0, new ListItem("--请选择--", "0"));
        }


        protected string GetPreferencesValue()
        {
            return CustomerManageHelper.GetPreferencesValue();
        }

        protected string GetPreferencesLevel()
        {
            return CustomerManageHelper.GetPreferencesLevel();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            this.DVCustomer.Visible = false;
            string name = this.txtCustomerName.Text;
            string company = this.txtCompanyName.Text;
            string title = this.txtTitle.Text;
            string level = this.selectlevel.SelectedItem.Value;
            string definition = this.Request.Form["selectDefination"];
            string country = this.Request.Form["selectCountry"];
            string editer = this.txtEdit.Text;
            string customerIDs1 = SearchHelper.GetCustomerIDsMain(name, company, title, level, definition,null, country, editer);

            string prelist = this.Request.Form["PreferenceList"];
            string prevalue = this.Request.Form["PreferenceValue"];
            string prelevel = this.Request.Form["PreferenceLevel"];
            string customerIDs2 = SearchHelper.GetCustomerIDsPrefernce(prelist, prevalue, prelevel);

            string degree = this.selectDegree.SelectedItem.Text;
            string school = this.txtSchool.Text;
            string customerIDs3 = SearchHelper.GetCustomerIDsEducation(degree, school);

            string workExpname = this.txtOldCompanyName.Text;
            string workExptitle = this.txtOldTitle.Text;
            string customerIDs4 = SearchHelper.GetCustomerIDsWorkExperience(workExpname, workExptitle);

            Search = "!it.Disabled";
            if (customerIDs1 != "0")
                Search += " and it.CustomerID in {" + customerIDs1 + "}";
            if (customerIDs2 != "0")
                Search += " and it.CustomerID in {" + customerIDs2 + "}";
            if (customerIDs3 != "0")
                Search += " and it.CustomerID in {" + customerIDs3 + "}";
            if (customerIDs4 != "0")
                Search += " and it.CustomerID in {" + customerIDs4 + "}";

            this.edsCustomer.Where = Search;
            this.GVCustomer.Visible = true;
            this.DVCustomer.Visible = false;
            this.GVCustomer.PageIndex = 0;
            
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            this.GVCustomer.Visible = false;
            this.DVCustomer.Visible = false;
            this.txtCompanyName.Text = "";
            this.txtCustomerName.Text = "";
            this.txtEdit.Text = "";
            this.txtOldCompanyName.Text = "";
            this.txtOldTitle.Text = "";
            this.txtSchool.Text = "";
            this.txtTitle.Text = "";
            this.selectCountry.SelectedIndex = 0;
            this.selectlevel.SelectedIndex = 0;
            this.selectDegree.SelectedIndex = 0;
            this.PreferenceList.SelectedIndex = 0;
            this.PreferenceValue.SelectedIndex = 0;
            this.PreferenceLevel.SelectedIndex = 0;
        }

        protected void GVCustomer_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "open")
            {
                int index = int.Parse(e.CommandArgument as string) + GVCustomer.PageIndex * GVCustomer.PageSize;
                DVCustomer.PageIndex = index;
                DVCustomer.Visible = true;

            }
        }

        protected void PreferenceList_SelectedIndexChanged(object sender, EventArgs e)
        {
            int listID = int.Parse(this.PreferenceList.SelectedValue);
            if (listID == 0)
            {
                PreferenceValue.SelectedIndex = 0;
                PreferenceLevel.SelectedIndex = 0;
                return;
            }
            //PreferenceValue
            PreferenceValue.DataSource = SearchHelper.GetPreferenceValues(listID);
            PreferenceValue.DataValueField = "ValueID";
            PreferenceValue.DataTextField = "Name";
            PreferenceValue.DataBind();
            PreferenceValue.Items.Insert(0, new ListItem("--请选择--", "0"));

            //PreferenceLevel
            PreferenceLevel.DataSource = SearchHelper.GetPreferenceLevels(listID);
            PreferenceLevel.DataValueField = "LevelID";
            PreferenceLevel.DataTextField = "Name";
            PreferenceLevel.DataBind();
            PreferenceLevel.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        protected IEnumerable GetEducations(object source)
        {
            return from t in source as IEnumerable<Education>
                   select t;
        }

        protected IEnumerable GetFamilyinfos(object source)
        {
            return from t in source as IEnumerable<FamilyInfo>
                   select t;
        }

        protected IEnumerable GetWorkExperiences(object source)
        {
            return from t in source as IEnumerable<WorkExperience>
                   select t;
        }

        protected IEnumerable GetCustomerPreference(object source)
        {
            return from t in source as IEnumerable<CustomerPreference>
                   select new
                   {
                       t.LevelID,
                       t.ValueID,
                       t.ListID,
                       ListName = GetListName(t),
                       LevelName = GetLevelName(t),
                       ValueName = GetValueName(t),

                   };
        }

        private string GetListName(CustomerPreference t)
        {
            t.PreferencesListReference.Load();
            return t.PreferencesList.Name;
        }

        private string GetValueName(CustomerPreference t)
        {
            t.PreferencesValueReference.Load();
            return t.PreferencesValue.Name;
        }

        private string GetLevelName(CustomerPreference t)
        {
            t.PreferencesLevelReference.Load();
            return t.PreferencesLevel.Name;
        }

        protected object EvalX(string expression)
        {
            CheckPageExists();

            var ctd = base.Page.GetDataItem() as ICustomTypeDescriptor;
            if (ctd != null)
            {
                return DataBinder.Eval(ctd.GetPropertyOwner(null), expression);
            }
            else
                return null;
        }

        private void CheckPageExists()
        {
            if (base.Page == null)
            {
                throw new InvalidOperationException("TemplateControl_DataBindingRequiresPage");
            }
        }



    }
}
